package com.zs4sdk.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.common.library.data.CommonEventHandler;
import com.common.library.data.CommonHandle;
import com.common.library.data.EventHandler2Get;
import com.common.library.entity.JSONResultEntity;
import com.common.library.util.ActivityHelper;
import com.common.library.util.CheckChineseNameUtil;
import com.common.library.util.CheckIdCardUtil;
import com.common.library.util.EncryptHelper;
import com.common.library.util.HandlerUtil;
import com.common.library.util.NetworkHelper;
import com.common.library.util.ParamsHelper;
import com.zs4sdk.common.Extra;
import com.zs4sdk.entity.Zs4GameData;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Jancy on 16/5/26.
 */
public class VerifyIdCardDialog extends Dialog implements View.OnClickListener{

    private Context context;
    private TextView titleTextView;

    private TextView verifyTitleTipTextView;
    // 提示
    private TextView verifyTipTextView;
    // 手机号码输入框
    private EditText nameEditText;
    // 手机验证码输入框
    private EditText idNumberEditText;
    private String backSendMsg = Extra.BOOLEAN_FALSE;
    private boolean isForPay = false;
    private boolean isLogin = true;
    private boolean isLoginVerify = false;


    private VerifyIdCardDialogListener listener;
    private VerifyIdCardDialogLoginListener loginListener;

    public interface VerifyIdCardDialogListener{
        public void onClick(View view);
    }

    public interface VerifyIdCardDialogLoginListener{
        public void onClick(boolean isverify);
    }

    public VerifyIdCardDialog(Context context, boolean isForPay) {
        super(context, ActivityHelper.getStyleResId("zs4gamemisc_text_dialog"));
        // TODO Auto-generated constructor stub
        this.context = context;
        this.isForPay = isForPay;
        isLogin = false;
    }

    public VerifyIdCardDialog(Context context,boolean isForPay,VerifyIdCardDialogLoginListener loginListener) {
        super(context,ActivityHelper.getStyleResId("zs4gamemisc_text_dialog"));
        // TODO Auto-generated constructor stub
        this.context = context;
        this.isForPay = isForPay;
        this.loginListener = loginListener;
        isLogin = false;
    }

    public VerifyIdCardDialog(Context context, boolean isForPay, VerifyIdCardDialogListener listener) {
        super(context,ActivityHelper.getStyleResId("zs4gamemisc_text_dialog"));
        // TODO Auto-generated constructor stub
        this.context = context;
        this.listener = listener;
        isLogin = true;
    }

    public VerifyIdCardDialog(Context context, boolean isForPay, VerifyIdCardDialogListener listener, VerifyIdCardDialogLoginListener loginListener) {
        super(context,ActivityHelper.getStyleResId("zs4gamemisc_text_dialog"));
        // TODO Auto-generated constructor stub
        this.context = context;
        this.listener = listener;
        this.loginListener = loginListener;
        isLogin = true;
        isLoginVerify = true;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(ActivityHelper.getLayoutResId("zs4gamelayout_dialog_verify_indentity"));
        initReferences();
    }

    // 设置界面
    private void initReferences() {
        titleTextView  = (TextView)findViewById(ActivityHelper.getIdResId("zs4gameviewid_mTextView_view_titlebar"));
        titleTextView.setText(ActivityHelper.getStringResId("zs4gamestring_account_title_verify_id"));

        verifyTitleTipTextView = (TextView)findViewById(ActivityHelper.getIdResId("zs4gameviewid_act_txv_verify_id_tip"));
        //提示
        verifyTipTextView = (TextView)findViewById(ActivityHelper.getIdResId("zs4gameviewid_txv_verify_id_tip"));
        //手机号
        nameEditText = (EditText)findViewById(ActivityHelper.getIdResId("zs4gameviewid_edt_input_name"));
        //验证码
        idNumberEditText = (EditText)findViewById(ActivityHelper.getIdResId("zs4gameviewid_edt_input_idnumber"));

        if(isForPay){
            verifyTitleTipTextView.setText(ActivityHelper.getStringResId("zs4gamestring_account_verify_id_tip_for_pay"));
            ((Button)findViewById(ActivityHelper.getIdResId("zs4gameviewid_btn_verify_id_nexttime"))).setVisibility(View.GONE);
        }else {
            verifyTitleTipTextView.setText(ActivityHelper.getStringResId("zs4gamestring_account_verify_id_tip"));
            ((Button)findViewById(ActivityHelper.getIdResId("zs4gameviewid_btn_verify_id_nexttime"))).setVisibility(View.VISIBLE);
        }
        if(isLogin) {
            ((ImageView) findViewById(ActivityHelper.getIdResId("zs4gameviewid_mLeftView_view_titlebar"))).setOnClickListener(this);
            ((Button) findViewById(ActivityHelper.getIdResId("zs4gameviewid_btn_verify_id_nexttime"))).setOnClickListener(this);
        }else {
            ((ImageView) findViewById(ActivityHelper.getIdResId("zs4gameviewid_mLeftView_view_titlebar"))).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                }
            });

            ((Button) findViewById(ActivityHelper.getIdResId("zs4gameviewid_btn_verify_id_nexttime"))).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                }
            });
        }


        ((Button)findViewById(ActivityHelper.getIdResId("zs4gameviewid_btn_goto_verify"))).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onVerifyId();
            }
        });
    }



    /**
     * 点击认证  进行判断
     */
    private void onVerifyId() {

        if (NetworkHelper.isNetworkAvailable(context)){
            String name = nameEditText.getText().toString().trim();
            String verifyIdCode = idNumberEditText.getText().toString().trim();
            if(name.equals("")||!CheckChineseNameUtil.isLegalName(name)){
                verifyTipTextView.setText(ActivityHelper.getStringResId("zs4gamestring_account_start_name"));
                return;
            }else  if(verifyIdCode.equals("")||!(new CheckIdCardUtil(verifyIdCode)).validate()){
                verifyTipTextView.setText(ActivityHelper.getStringResId("zs4gamestring_account_start_id_code"));
                return;
            }else {
                verifyTipTextView.setText("");
                httpGetVerifyIdNumber(name, verifyIdCode);
            }
        } else {
            verifyTipTextView.setText(ActivityHelper.getStringResId("zs4gamestring_hint_has_not_network"));
        }
    }


    /**
     * 请求认证（117）
     *
     * @param name
     * @param verifyIdCode
     */
    private void httpGetVerifyIdNumber(final String name, final String verifyIdCode) {

        ArrayList<NameValuePair> paramList = new ArrayList<NameValuePair>();
        paramList.add(new BasicNameValuePair("name", name));
        paramList.add(new BasicNameValuePair("id_number", EncryptHelper.encryptByAES(verifyIdCode)));
        new EventHandler2Get(
                Extra.URL.VERIFY_ID_CARD,
                paramList,
                ParamsHelper.getUserAgent1(),
                new CommonEventHandler.ResultHandlerInEvent() {

                    @Override
                    public void doResultHanler(JSONObject result) throws JSONException {

                        HandlerUtil.sendMessage(myHandle, Extra.MessageKey.DISMISSPROCESS);

                        Message msg = myHandle.obtainMessage();

                        msg.what = Extra.MessageKey.REQUEST_VERIFY_SUCCESS;
                        msg.obj = result;
                        // 发送消息
                        myHandle.sendMessage(msg);

                    }

                    @Override
                    public void doExceptionHandler() {
                        HandlerUtil.sendMessage(myHandle, Extra.MessageKey.DISMISSPROCESS);
                    }
                });
    }


    CommonHandle myHandle = new CommonHandle(context, new CommonHandle.HandleMessage() {

        @Override
        public void doHandleMessage(Message msg) {

            if (Extra.MessageKey.REQUEST_VERIFY_SUCCESS == msg.what) {

                JSONResultEntity entity = new JSONResultEntity((JSONObject) msg.obj);
                if (entity.isSuccess()) {
                    Zs4GameData.getInstance().getLoginJSONResultEntity().setIsverify("1");
                    ActivityHelper.showToast("认证成功");
                    if(isLoginVerify) {
                       loginListener.onClick(true);
                    }
                    dismiss();
                } else {
                    verifyTipTextView.setText(entity.getDesc());
                }

            }
        }
    });

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        listener.onClick(v);
    }

    @Override
    public void onBackPressed() {
    }

}
